﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;


namespace WirtualneNauczanie.Web
{
    public partial class ChooseCourseType : System.Web.UI.Page
    {
        ListBox ListBox2;
        protected void Page_Load(object sender, EventArgs e)
        {
            kw1();
            /*
            //Pobranie ID usera
            MembershipUser myObject = Membership.GetUser();
            string StudentID = myObject.ProviderUserKey.ToString();

            //Pobranie wszystkich ID kursow w ktorych jest zapisany uzytkownik
            SqlDataSource1.SelectCommand = "SELECT Courses.CourseID FROM Courses INNER JOIN StudentsCourses ON Courses.CourseID = StudentsCourses.CourseID WHERE (StudentsCourses.StudentID = '" + StudentID + "')";
            DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
            int numOfRows = dv.Count;
            
            //BazaDataContext db = new BazaDataContext();

            //var courseIDs = from sc in db.StudentsCourses where sc.StudentID.Equals(StudentID) select sc.CourseID;
            //Console.WriteLine(courseIDs.Count());
            
            //LinqDataSource1.
            

            //int numOfRows = LinqDataSource1;

            String sqlSelectQuery = "SELECT CourseID, CourseName, CourseDescription FROM Courses";
            if (numOfRows > 0) { sqlSelectQuery = sqlSelectQuery + " WHERE "; }
            for (int i = 0; i < numOfRows; i++)
            {
                sqlSelectQuery += " (CourseID <> " + (int)dv.Table.Rows[i][0] + ") ";       // = (int)dv.Table.Rows[0][0];
                if(i < (numOfRows-1)) sqlSelectQuery += " AND ";
            }
            SqlDataSource1.SelectCommand = sqlSelectQuery;
            ListBox2 = new ListBox();
            ListBox2.DataSource = SqlDataSource1;
            ListBox2.DataTextField = "CourseName";
            ListBox2.DataValueField = "CourseID";            
            ListBox2.DataBind();
            PlaceHolder1.Controls.Add(ListBox2);*/

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
        {

        }

        private void kw1()
        {
            DataClasses1DataContext db = new DataClasses1DataContext();

            MembershipUser myObject = Membership.GetUser();
            string StudentID = myObject.ProviderUserKey.ToString();

            var query1 = from c in db.Courses
                         select new
                         {
                             CourseID = c.CourseID,
                             CourseName = c.CourseName,
                             CourseDescription = c.CourseDescription
                         };

            var query2 = from c in db.Courses
                         join sc in db.StudentsCourses on c.CourseID equals sc.CourseID
                         where sc.StudentID.Equals(StudentID)
                         select new
                         {
                             CourseID = c.CourseID,
                             CourseName = c.CourseName,
                             CourseDescription = c.CourseDescription
                         };

            var query3 = query1.Except(query2);


            ListBox2 = new ListBox();
            foreach (var course in query3)
            {
                String id = Convert.ToString(course.CourseID);
                String name = course.CourseName;                
                ListBox2.Items.Add(new ListItem(name, id));

            }
            PlaceHolder1.Controls.Add(ListBox2);
        }

        private void kw2()
        {
            DataClasses1DataContext db = new DataClasses1DataContext();

            MembershipUser myObject = Membership.GetUser();
            string StudentID = myObject.ProviderUserKey.ToString();

            //pobranie max id z tabeli SC
            int maxID = db.StudentsCourses.Max(sc => sc.StudentCourseID);
            Label1.Text = Convert.ToString(maxID);

            //Dodanie studenta do kursu
            StudentsCourse course = new StudentsCourse
            {
                StudentCourseID = maxID + 1,
                CourseID = Convert.ToInt32(ListBox2.SelectedValue),
                DateEnrolled = System.DateTime.Today,
                StudentID = new Guid(StudentID)
            };
            db.StudentsCourses.InsertOnSubmit(course);
            db.SubmitChanges();

        }



        protected void Button1_Click(object sender, EventArgs e)
        {

            kw2();
            /*
            //Pobranie Maksymalnego ID tabeli StudentsCourses            
            int StudentCourseID = 0;
            SqlDataSource2.SelectCommand = "SELECT MAX(StudentCourseID) FROM StudentsCourses";
            DataView dv = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);

            if (dv.Table.Rows[0][0].Equals(System.DBNull.Value))
            {
                StudentCourseID = 1;
            }
            else
            {
                StudentCourseID = (int)dv.Table.Rows[0][0];
            }

            //Pobranie Id kursu
            
            String CourseID = ListBox2.SelectedValue;            
                                 
            //Pobranie dzisiejszej daty
            DateTime DateEnrolled = System.DateTime.Today;

            //Pobranie ID usera
            MembershipUser myObject = Membership.GetUser();
            string StudentID = myObject.ProviderUserKey.ToString();

            //Dodanie Studenta do określonego kursu
            SqlDataSource1.InsertCommand = "INSERT INTO StudentsCourses (StudentCourseID, CourseID, DateEnrolled, StudentID) VALUES " +
                 " ('" + (StudentCourseID + 1) + "','" + CourseID + "','" + DateEnrolled + "','" + StudentID + "')";
            SqlDataSource1.Insert();

            Label1.Text = "Wartość ID Max: " + CourseID + " " + (StudentCourseID + 1) + " " + StudentID + " " + DateEnrolled;*/
            Server.Transfer("LoggedIn.aspx");

        }

    }
}
